package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.spi.http.HttpContext;
import java.io.IOException;
import java.util.Date;

@WebServlet("/neworder")
public class NewOrderServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            /*
             HttpSession session = context.getRequest().getSession();

    if (shippingAddressRequired) {
      shippingAddressRequired = false;
      return new ForwardResolution(SHIPPING);
    } else if (!isConfirmed()) {
      return new ForwardResolution(CONFIRM_ORDER);
    } else if (getOrder() != null) {

      orderService.insertOrder(order);

      CartActionBean cartBean = (CartActionBean) session.getAttribute("/actions/Cart.action");
      cartBean.clear();

      setMessage("Thank you, your order has been submitted.");

      return new ForwardResolution(VIEW_ORDER);
    } else {
      setMessage("An error occurred processing your order (order was null).");
      return new ForwardResolution(ERROR);
    }
             */
        Order order =(Order) req.getSession().getAttribute("order");
        String confirm = req.getParameter("confirm");
        String billfirstname = req.getParameter("order.billToFirstName");
        String billlastname = req.getParameter("order.billToLastName");
        String billcity = req.getParameter("order.billCity");
        String billzip = req.getParameter("order.billZip");
        String billcountry = req.getParameter("order.billCountry");
        String billstate = req.getParameter("order.billState");
        String billaddress1 = req.getParameter("order.billAddress1");
        String billaddress2 = req.getParameter("order.billAddress2");
        String shipfirstname = req.getParameter("order.shipToFirstName");
        String shiplastname = req.getParameter("order.shipToLastName");
        String shipcity = req.getParameter("order.shipCity");
        String shipzip = req.getParameter("order.shipZip");
        String shipstate = req.getParameter("order.shipState");
        String shipcountry = req.getParameter("order.shipCountry");
        String shipaddress1 = req.getParameter("order.shipAddress1");
        String shipaddress2 = req.getParameter("order.shipAddress2");
        if (billfirstname!=null){
            order.setBillToFirstName(billfirstname);
        }
        if (billlastname!=null){
            order.setBillToLastName(billlastname);
        }
        if (billcity!=null){
            order.setBillCity(billcity);
        }
        if (billcountry!=null){
            order.setBillCountry(billcountry);
        }
        if (billzip!=null){
            order.setBillZip(billzip);
        }
        if (billstate!=null){
            order.setBillState(billstate);
        }
        if (billaddress1!=null){
            order.setBillAddress1(billaddress1);
        }
        if (billaddress2!=null){
            order.setBillAddress2(billaddress2);
        }
        if (shipfirstname!=null){
            order.setShipToFirstName(shipfirstname);
        }
        if (shiplastname!=null){
            order.setShipToLastName(shiplastname);
        }
        if (shipcity!=null){
            order.setShipCity(shipcity);
        }
        if (shipstate!=null){
            order.setShipState(shipstate);
        }
        if (shipzip!=null){
            order.setShipZip(shipzip);
        }
        if (shipcountry!=null){
            order.setShipCountry(shipcountry);
        }
        if (shipaddress1!=null){
            order.setShipAddress1(shipaddress1);
        }
        if (shipaddress2!=null){
            order.setShipAddress2(shipaddress2);
        }
        OrderService orderService = new OrderService();
        String shippingAddressRequired = req.getParameter("shippingAddressRequired");
        System.out.println("shippingnot");
        System.out.println(shippingAddressRequired);
        if (shippingAddressRequired!=null) {
            System.out.println("shipping");
            shippingAddressRequired = null;
            req.getRequestDispatcher("/WEB-INF/jsp/order/ShippingForm.jsp").forward(req, resp);
        }else if (confirm == null) {
            req.getRequestDispatcher("/WEB-INF/jsp/order/ConfirmOrder.jsp").forward(req, resp);
        }else if (req.getSession().getAttribute("order")!=null){

            orderService.insertOrder(order);
            System.out.println("okokok");
            CatalogService catalogService = new CatalogService();
            //购物车清空
            catalogService.clearcart(order.getUsername());
            req.getSession().removeAttribute("cart");
            LogService logService = new LogService();
            String loginmsg = "生成订单"+order.getOrderId();
            Date time = new Date();
            logService.insertLog_signin(order.getUsername(),loginmsg,time);


            req.getRequestDispatcher("/WEB-INF/jsp/order/ViewOrder.jsp").forward(req,resp);
        }


    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
